/*document.write("<script src='"+CTX+"/javascripts/caCert/jquery.datePicker-min.js'><\/script>");
document.write("<script src='"+CTX+"/javascripts/caCert/jquery.cookie.js'><\/script>");
document.write("<script src='"+CTX+"/javascripts/caCert/TopESA3.1.0.4.20242.js'><\/script>");
document.write("<script src='"+CTX+"/javascripts/gopay/security/caCert/CaCertSign.js'><\/script>");
*/
var portalUrl;
function initCertCheck(divId) {
	
	
	var url = ctx+ "/sec/caCert/checkCaCert.shtml";
	$.post(url, null, function(data) {
		if ("00" == data.stat || "02" == data.stat || "03" == data.stat) {
			 var loginExFirstPay = $("#loginExFirstPay").val();
			 //不是首次快捷支付，不用密码控件
			 if(loginExFirstPay!="01"){
				 CaCertSign.usePwd = true;
			 }
			
		} else if ("01" == data.stat) {
			CaCertSign.sn = data.sn;
			portalUrl = data.portalUrl;
			var config = {
					"license" : "MIIFvQYJKoZIhvcNAQcCoIIFrjCCBaoCAQExDjAMBggqgRzPVQGDEQUAMIIBAgYJKoZIhvcNAQcBoIH0BIHxeyJJc3N1ZXIiOiIoKCguKkNOPeWbveS7mOWuneS/oeaBr+enkeaKgOaciemZkOWFrOWPuENBLiopfCguKk9VPeS6p+WTgeaKgOacr+mDqC4qKXwoLipPPeWbveS7mOWuneS/oeaBr+enkeaKgOaciemZkOWFrOWPuC4qKSl7M30pIiwidmVyc2lvbiI6IjEuMC4wLjEiLCJzb2Z0VmVyc2lvbiI6IjMuMS4wLjAiLCJub3RhZnRlciI6IjIwMjEtMDItMTYiLCJub3RiZWZvcmUiOiIyMDE2LTAyLTE2Iiwibm9BbGVydCI6InRydWUifaCCA0QwggNAMIIC5aADAgECAhRfJZzazIK7fvMFswB7i47Jvfm18jAMBggqgRzPVQGDdQUAMFUxJjAkBgNVBAMMHeWkqeivmuWuieS/oea1i+ivlVNNMueUqOaIt0NBMQ4wDAYDVQQLDAVUT1BDQTEOMAwGA1UECgwFVE9QQ0ExCzAJBgNVBAYTAkNOMB4XDTE0MDkyNjA3NDYwOFoXDTE1M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AHuLjsm9+bXyMAwGCCqBHM9VAYMRBQCgaTAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3DQEJBTEPFw0xNTAyMTYxMjUzMzZaMC8GCSqGSIb3DQEJBDEiBCDqwd46/v16IZJU9X6L5ICsbNpuUp/E9F9cDkCgoi5qRzAMBggqgRzPVQGCLQUABEYwRAIgtEiqCZK5FR+RjVabU0VS1yo0Wh2wYFYrz1v1we1ie0wCIFDRf32RrTID8WyywPF1EDrBkacn3umyykOeEo7jEF8j",
					"exepath" : portalUrl+ "/resources/download/天诚安信证书助手.exe"
				};
			try {
				TCA.config(config);
				CaCertSign.toSign = data.sign;
				var certs = CertStore.listAllCerts();
				var r = certs.bySerialnumber(CaCertSign.sn);
				CaCertSign.signedData = r.get(0).signMessage(CaCertSign.toSign, false)
				CaCertSign.signedData = encodeURI(CaCertSign.signedData).replace(/\+/g,"wangdakui");
				CaCertSign.usePwd = true;
				CaCertSign.caStat = 1;
			} catch (e) {
				if (e instanceof TCACErr) {
					CaCertSign.caStat = 3;
					alert(e.toStr());
				} else {
					CaCertSign.caStat = 3;
					alert("数字证书获取失败");
				}
			}
		} else {
			CaCertSign.caStat = 3;
			alert("数字证书获取失败");
		}
		//不是首次快捷时，才显示
		if (!CaCertSign.usePwd && loginExFirstPay!="01") {
			$("#"+divId).html('<a style="text-decoration:underline" href="javascript:goInstall()">安装数字证书 </a>（如果您已申请过数字证书，但是本机未安装，请点击此按钮）');
		}
		initPwdControl();
		//增加一回调函数
		if(typeof(afterCertCheck)!="undefined"){
			afterCertCheck(CaCertSign.caStat); 
		  }

	}, "json");

	$('.date-pick').datePicker({
		clickInput : true
	});
};

function goInstall(){
	$.confirm("您是否已成功安装数字证书？",function(r){
		window.location.reload(true);
	});
	
	window.open(portalUrl+'/security/caCert/toInstall.shtml');
}

function checkCaCert(divId) {
	var stat = false;
	try {
		var certs = CertStore.listAllCerts();
		var r = certs.bySerialnumber(CaCertSign.sn);
		CaCertSign.signedData = r.get(0).signMessage(CaCertSign.toSign, false)
		CaCertSign.signedData = encodeURI(CaCertSign.signedData).replace(/\+/g,"wangdakui");
		stat = true;
	} catch (e) {
		CaCertSign.usePwd = false;
		$("#"+divId).html('<a style="text-decoration:underline" href="javascript:goInstall()">安装数字证书 </a>（如果您已申请过数字证书，但是本机未安装，请点击此按钮）');
		if (e instanceof TCACErr) {
			alert(e.toStr());
		} else {
			alert("数字证书获取失败");
		}
	}
	return stat;
}
